<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_product_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','name','title','group_id','price','createtime','readnum','status','picurl');
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('core_account_product',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['group_id']=$fenlei[$item['group_id']]['title'];
			$item['url']=custom_url('core/product.list/detail/id='.$item['id'],true);
			
		}
	}
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['fenlei']=$fenlei?$fenlei:array();
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'产品列表'),
			array('op'=>'edit','title'=>'添加产品'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'产品标题','type'=>'string'),
			array('name'=>'group_id','title'=>'产品分类','type'=>'string'),
			array('name'=>'price','title'=>'产品价格','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)
		),
		'search_one'=>array('name'=>'title','help'=>''),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['groups']=pdo_getall('core_account_product_fenlei',$params,array('id','title'));
	$return['biaoqian']=pdo_get('core_account_product_biaoqian',$params,array('bqs'));
	$return['biaoqian']=explode(' ',$return['biaoqian']['bqs']);
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_product',array('id'=>$id));
		$item['slide']=$item['slide']?json_decode($item['slide'],true):$item['slide'];
	}else{
		$item=array();
	}
	
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'产品信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'产品列表'),
			array('op'=>'edit','title'=>'添加产品'),
		),
		'edit'=>array(
			'0'=>array('type'=>'string','title'=>'产品名称','name'=>'title' ),
			'1'=>array('type'=>'image','title'=>'logo','name'=>'picurl' ),
			'2'=>array('type'=>'image','title'=>'二维码','name'=>'qrcode' ),
			'3'=>array('type'=>'images','title'=>'产品图片','name'=>'slide' ),
			'4'=>array('type'=>'switch','title'=>'是否开启','name'=>'status' ),
			'5'=>array('type'=>'select','title'=>'产品分类','name'=>'group_id', 'options'=>'groups'),
			'6'=>array('type'=>'checkbox','title'=>'产品标签','name'=>'biaoqian', 'options'=>'biaoqian'),
			'7'=>array('type'=>'string','title'=>'产品价格','name'=>'price' ),
			'9'=>array('type'=>'text','title'=>'产品介绍','name'=>'desc' ),
			'10'=>array('type'=>'richtext','title'=>'产品内容','name'=>'content' ),
			'11'=>array('type'=>'switch','title'=>'是否推荐','name'=>'tuijian' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'title'=>trim($post['title']),
		'picurl'=>trim($post['picurl']),
		'qrcode'=>trim($post['qrcode']),
		'slide'=>ijson_encode($post['slide']),
		'status'=>intval($post['status']),
		'group_id'=>intval($post['group_id']),
		'biaoqian'=>trim($post['biaoqian']),
		'price'=>trim($post['price']),
		'desc'=>trim($post['desc']),
		'content'=>$post['content'],
		'tuijian'=>trim($post['tuijian']),
	);
	$id=intval($post['id']);
	if($id){
		pdo_update('core_account_product',$data,array('id'=>$id));
	}else{
		$data['createtime']=TIMESTAMP;
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('core_account_product',$data);
	}
	exi('保存成功！');
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('core_account_product',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_product',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_product',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}